#include "Root.h"

Root* Root::root_ = NULL;

Root::Root() : activeEventHandler_(NULL)
{
    activeEventHandler_ = NULL;
    eventHandlers_.clear();
}

Root::~Root()
{
    for (int i = 0; i < eventHandlers_.size(); i++)
    {
        delete eventHandlers_.at(i);
    }
    eventHandlers_.clear();
}

void Root::AddEventHandler(EventHandler* eventHandler) {
    LogUtil::Instance()->logInfo("Root", "AddEventHandler", "Adding event handler");
    eventHandler->SetManager(this);
    eventHandlers_.push_back(eventHandler);
}

EventHandler& Root::GetActiveEventHandler() {
    return (*activeEventHandler_);
}

void Root::SetActiveEventHandler(const int& pos) {
    activeEventHandler_ = eventHandlers_.at(pos);
}

void Root::SetActiveEventHandler(EventHandler* eventHandler) {
    activeEventHandler_ = eventHandler;
}

void Root::DeleteEventHandler(const int& pos) {
    if (eventHandlers_.at(pos) != activeEventHandler_) {
        delete eventHandlers_.at(pos);
        eventHandlers_.erase(eventHandlers_.begin() + pos);

    } else {
        std::cout<<"Cannot delete active event handler."<<std::endl;
    }
}

void Root::Initialize(int& argc, char**& argv)
{
    
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_ALPHA | GLUT_DEPTH | GLUT_STENCIL);
    glutInitWindowPosition(10, 10);
    glutInitWindowSize(ConfigUtil::Instance()->GetScreenWidth(), 
                       ConfigUtil::Instance()->GetScreenHeight());
    glutCreateWindow(argv[0]);

    glutReshapeFunc(Reshape);
    glutDisplayFunc(Display);
    glutKeyboardFunc(Keyboard);
    glutEntryFunc(Entry);
    glutMouseFunc(Mouse);
    glutMotionFunc(Motion);
    glutPassiveMotionFunc(PassiveMotion);
    glutIdleFunc(Idle);
    atexit(Shutdown);
    Startup();

    if (activeEventHandler_ != NULL)
    {
        glutMainLoop();
    }
}

void Root::Reshape(int width, int height)
{
    Instance()->GetActiveEventHandler().OnReshape(width, height);
}

void Root::Display()
{
    Instance()->GetActiveEventHandler().OnDisplay();
    glutSwapBuffers();
}

void Root::Keyboard(unsigned char key, int x, int y)
{
    /*GlutSingleton::Instance().input_.keyStates_[key] = 
        !GlutSingleton::Instance().input_.keyStates_[key];
    GlutSingleton::Instance().GetActiveEventHandler().OnKeyboard(key, x, y);*/
}

void Root::Entry(int state)
{
  /*  GlutSingleton::Instance().GetActiveEventHandler().OnEntry(state);*/
}

void Root::Mouse(int button, int state, int x, int y)
{
    //GlutSingleton::Instance().GetActiveEventHandler().OnMouse(button, state, x, y);
}

void Root::Motion(int x, int y)
{
    //GlutSingleton::Instance().GetActiveEventHandler().OnMotion(x, y);
}

void Root::PassiveMotion(int x, int y)
{
    //GlutSingleton::Instance().GetActiveEventHandler().OnPassiveMotion(x, y);
}

void Root::Idle()
{
    /*GlutSingleton::Instance().GetActiveEventHandler().OnIdle();
    glutPostRedisplay();*/
}

void Root::Shutdown()
{
    
}

void Root::Startup()
{
    
}